package com.yanqu.road.server.gameplayer.module.activity.cat.action;

import com.yanqu.road.entity.activity.cat.*;
import com.yanqu.road.entity.activity.cat.config.CatNpcInfo;
import com.yanqu.road.server.gameplayer.module.activity.cat.CatActivityConfig;
import com.yanqu.road.server.gameplayer.module.activity.cat.ConnectResult;
import com.yanqu.road.server.manger.activity.cat.CatMgr;
import com.yanqu.road.server.pb.CatActivityPb;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 小怪元素
 *
 * 形、真、理
 */
public class MonsterElementAction extends BaseElementAction {

    public MonsterElementAction(int type) {
        super(type);
    }

    @Override
    boolean accept(Element element, Element next, ConnectResult context) {
        //下一个是小怪
        if(next.getType() == eElementTypes.Monster.getType()){
            //连接数要大于血量
            if(next.getVal() > context.getCurAttackPoint()){
                context.setRet(-1);
                return false;
            }
            //CatMgr.getLogger().info("-------- x:{},y:{}, 小怪——>小怪，扣血：{}", next.getX(), next.getY(), next.getVal());
            context.setOffsetPoint(context.getOffsetPoint() + next.getVal());
        }

        //下一个是监牢
        EleAttr attr = CatMgr.hasAttr(next, eElementAttrTypes.Prison.getType());
        if(attr != null){
            //连接数要大于血量
            if(attr.getVal() > context.getCurAttackPoint()){
                context.setRet(-1);
                return false;
            }
            //CatMgr.getLogger().info("-------- x:{},y:{}, 小怪——>监牢，扣血：{}", next.getX(), next.getY(), attr.getVal());
            context.setOffsetPoint(context.getOffsetPoint() + attr.getVal());
        }

        if(CatMgr.isBlock(next)){
            context.setRet(-1);
            return false;
        }
        return super.acceptNext(context);
    }

    @Override
    void connect(Element element, ConnectResult context) {
        CatActivityConfig config = CatMgr.getConfig();
        if(config != null){
            CatNpcInfo npcInfo = config.getCatNpcInfoById(element.getId());
            if(npcInfo != null && npcInfo.getSkillType() == 5){
                //杀死沉默怪
                context.getGameData().setKillSilent(true);
            }
        }
        CatMgr.clearElementTypeInfo(element);
        CatMgr.removeElementAttr(element, eElementAttrTypes.Survive.getType());
        super.connectNext(context);
    }

    @Override
    void change(Element element, ConnectResult context) {
        //存活回数加1
        EleAttr attr = CatMgr.hasAttr(element, eElementAttrTypes.Survive.getType());
        if(attr != null){
            attr.setVal(1 + attr.getVal());
        }
        DharmaElementAction.onChange(element, context);
        super.changeNext(context);
    }

    @Override
    void attack(Element element, ConnectResult context) {
        //小怪类型
        int monsterType;
        CatNpcInfo npcInfo = null;
        CatActivityConfig config = CatMgr.getConfig();
        if(config != null){
            npcInfo = config.getCatNpcInfoById(element.getId());
        }
        if(npcInfo != null){
            monsterType = npcInfo.getSkillType();
            if(monsterType > 0){
                //攻击周围8个方向中的主角（每个小怪都有该技能）  普通技能1
                List<Element> elementList = CatMgr.getEightDirection(context.getElements(), new Position(element.getX(), element.getY()));
                for (Element tempEle : elementList) {
                    if(tempEle.getType() == eElementTypes.Role.getType()){
                        if(tempEle.getVal() > 0 && context.getGameData().getRoleBlood() > 0){
                            //记录  detail  给客户端
                            context.getDetailBuilderList().add(CatActivityPb.parseCatConnectDetailTemp(element.getX(), element.getY(), element.getType(), element.getId(), 1, 0));
                            tempEle.setVal(tempEle.getVal() - 1);
                            context.getGameData().setRoleBlood(context.getGameData().getRoleBlood() - 1);
                            //CatMgr.getLogger().info("--------- 攻击主角：{}，{}", element.getX(), element.getY());

                            //主角掉血日志
                            int times = context.getGameData().getRoleBloodLose().getOrDefault(1, 0);
                            context.getGameData().getRoleBloodLose().put(1, times + 1);

                            //小怪技能发动日志
                            int times2 = context.getGameData().getMonsterSkill().getOrDefault(1, 0);
                            context.getGameData().getMonsterSkill().put(1, times2 + 1);
                        }
                        break;
                    }
                }
            }
            if(monsterType == 2){
                EleAttr attr = CatMgr.hasAttr(element, eElementAttrTypes.Survive.getType());
                if(attr != null){
                    //三回合攻击一次主角  精准技能
                    int perRound = Integer.parseInt(npcInfo.getSkillParameter());
                    if(attr.getVal() % perRound == 0){
                        //CatMgr.getLogger().info("三回合攻击一次主角， 存活 {}", attr.getVal());
                        Element roleElement = CatMgr.getElement(context.getElements(), context.getRolePosition().getX(), context.getRolePosition().getY());
                        if(roleElement != null && roleElement.getVal() > 0 && context.getGameData().getRoleBlood() > 0){
                            //记录  detail
                            context.getDetailBuilderList().add(CatActivityPb.parseCatConnectDetailTemp(element.getX(), element.getY(), element.getType(), element.getId(), 1, 1));
                            roleElement.setVal(roleElement.getVal() - 1);
                            context.getGameData().setRoleBlood(context.getGameData().getRoleBlood() - 1);
                            //CatMgr.getLogger().info("--------- 攻击主角：{}，{}", element.getX(), element.getY());

                            //主角掉血日志
                            int times = context.getGameData().getRoleBloodLose().getOrDefault(2, 0);
                            context.getGameData().getRoleBloodLose().put(2, times + 1);

                            //小怪技能发动日志
                            int times2 = context.getGameData().getMonsterSkill().getOrDefault(2, 0);
                            context.getGameData().getMonsterSkill().put(2, times2 + 1);
                        }
                    }
                }
            }else if(monsterType == 3){
                //血债，使2个达摩愤怒
                int num = 2;
                List<Integer> skillParamList = StringUtils.stringToIntegerList(npcInfo.getSkillParameter(), ";");
                int perRound = skillParamList.get(0);
                if(skillParamList.size() == 2){
                    num = skillParamList.get(1);
                }
                EleAttr attr = CatMgr.hasAttr(element, eElementAttrTypes.Survive.getType());
                if(attr != null && attr.getVal() % perRound == 0){
                    //CatMgr.getLogger().info("血债，每 {} 回合使 {} 个达摩愤怒", perRound, num);
                    List<Integer> list = CatMgr.getRandomElementIndex();
                    while (num > 0 && list.size() > 0){
                        int index = list.remove(0);
                        Position position = CatMgr.coordinate(index);
                        Element target = CatMgr.getElementOrNull(context.getElements(), position.getX(), position.getY());
                        if(target != null){
                            if(target.getType() == eElementTypes.Dharma.getType() && CatMgr.hasAttr(target, eElementAttrTypes.Anger.getType()) == null){
                                attr = new EleAttr(eElementAttrTypes.Anger.getType(), 1);
                                target.getAttrs().add(attr);
                                num--;
                                //标记当前回合不可触发
                                context.getChangedList().add(CatMgr.getIndex(new Position(target.getX(), target.getY())));
                                //记录  detail
                                context.getDetailBuilderList().add(CatActivityPb.parseCatConnectDetailTemp(element.getX(), element.getY(), element.getType(), element.getId(), 1, target.getX(), target.getY()));
                                //CatMgr.getLogger().info("--------- {}，{}", target.getX(), target.getY());

                                //小怪技能发动日志
                                int times2 = context.getGameData().getMonsterSkill().getOrDefault(3, 0);
                                context.getGameData().getMonsterSkill().put(3, times2 + 1);
                            }
                        }
                    }
                }
            }else if(monsterType == 4){
                //给boss 回血
                EleAttr attr = CatMgr.hasAttr(element, eElementAttrTypes.Survive.getType());
                if(attr != null){
                    //间隔2回合
                    int addition = 300;
                    List<Integer> skillParamList = StringUtils.stringToIntegerList(npcInfo.getSkillParameter(), ";");
                    int perRound = skillParamList.get(0);
                    if(skillParamList.size() == 2){
                        addition = skillParamList.get(1);
                    }
                    if(config.getXinMoPassId() != context.getMissionInfo().getId()){
                        if(attr.getVal() % perRound == 0){
                            long max = Long.valueOf(context.getMissionInfo().getBossPower());
                            long add = BigDecimal.valueOf(max).multiply(BigDecimal.valueOf(addition))
                                    .divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).longValue();
                            long before = context.getGameData().getBossBlood();
                            if(add + before > max){
                                context.getGameData().setBossBlood(max);
                            }else {
                                context.getGameData().setBossBlood(add + before);
                            }
                            //CatMgr.getLogger().info("给boss 回血 {} to {} addition {}, 存活 {}, max {}", before, context.getGameData().getBossBlood(), addition, attr.getVal(), max);

                            //记录  detail
                            context.getDetailBuilderList().add(CatActivityPb.parseCatConnectDetailTemp(element.getX(), element.getY(), element.getType(), element.getId(), 1, 1));

                            //小怪技能发动日志
                            int times2 = context.getGameData().getMonsterSkill().getOrDefault(4, 0);
                            context.getGameData().getMonsterSkill().put(4, times2 + 1);
                        }
                    }else {
                        CatMgr.getLogger().info(" 心魔模式，不用回血");
                    }
                }
            }
        }
        super.attackNext(context);
    }

    @Override
    void generate(Element element, ConnectResult context) {

        super.generateNext(context);
    }
}
